import { Extension } from '@tiptap/core'

export const TabIndent = Extension.create({
  name: 'tabIndent',

  addKeyboardShortcuts() {
    return {
      Tab: ({ editor }) => {
          const dom = document.querySelector('.copilot-suggestion')
          if (dom) {
            // 有 AI 补全提示，不处理缩进
            return false
          }
        const { state, dispatch } = editor.view
        const { from, to } = state.selection
        const node = state.selection.$from.parent

        const canIndent = ['paragraph', 'codeBlock'].includes(node.type.name)

        if (canIndent) {
          dispatch(state.tr.insertText('\u00A0\u00A0', from, to))
          return true
        }

        console.warn('[Tab] 当前节点不支持缩进:', node.type.name)
        return false
      },
    }
  },
})